﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;

namespace Service
{
    public partial class ServiceMimoza : ServiceBase
    {
        public ServiceMimoza()
        {
            InitializeComponent();
        }

        public void Run(string[] args)
        {
            Init(args);

            m_thread = new Thread(new ThreadStart(DoWork));
            m_thread.Start();
        }

        public void Finish()
        {
            m_application.Stop();
            if (m_thread != null)
            {
                m_thread.Join();
            }
        }

        public void Init(string[] args)
        {
            Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("ru-RU");
            Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("ru-RU");

            Mimoza.ServiceLayer.Configuration configuration = new 
                Mimoza.ServiceLayer.Configuration();

            m_application = new Mimoza.Apps.Service.Application(configuration);
            m_application.Init();
        }
                
        protected void DoWork()
        {
            m_application.DoWork();
        }

        protected override void OnStart(string[] args)
        {
            Run(args);
        }

        protected override void OnStop()
        {
            Finish();            
        }

        Mimoza.Apps.Service.Application m_application = null;
        Thread m_thread;
    }
}
